package com.seaboxdata.interfaceJob;

import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.ConstructionVo;
import com.seaboxdata.entity.DpjfxtConstruction;
import com.seaboxdata.entity.FenceCoordinate;
import com.seaboxdata.entity.FenceCoordinateVo;
import com.seaboxdata.service.DpjfxtConstructionService;
import com.seaboxdata.service.DpjfxtTokenService;
import com.seaboxdata.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @create 2021-04-27 16:20
 **/
@Component
@Configuration
@Slf4j
public class DpjfxtConstructionJob {

    @Autowired
    private DpjfxtTokenService dpjfxtTokenService;

    @Autowired
    private DpjfxtConstructionService dpjfxtConstructionService;

    @Value("${dpjfxt.construction.url}")
    private String url;

    @Value("${interface.isTest}")
    private String isTest;

    @Value("${dpjfxt.construction.fenceCoordinate.url}")
    private String fenceCoordinateUrl;

    @Scheduled(cron = "${dpjfxt.construction.interval}")//每隔5分钟执行一次
    public void doCollect() {
        log.info("get construction begin");
        //如果是测试,直接退出
        if ("true".equals(isTest)) {
            return;
        }
        String token = dpjfxtTokenService.getToken();
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("content-type", "application/json");
        if (StringUtils.isEmpty(token)) {
            log.error("get token error");
            return;
        }
        headerMap.put("token", token);

        //获取工地围栏信息
        Map<String, String> fenceCoordinatesMap = new HashMap<>();
        HttpUtils.Response fenceCoordinateResponse = HttpUtils.doPost(fenceCoordinateUrl, new JSONObject().toJSONString(), "utf-8", headerMap);
        if (fenceCoordinateResponse.getStatusCode() == 200) {
            FenceCoordinateVo fenceCoordinateVo = JSONObject.parseObject(fenceCoordinateResponse.getResponseData(), FenceCoordinateVo.class);
            if (fenceCoordinateVo.getStatus() != 200) {
                log.error("get fenceCoordinate error,cause: " + fenceCoordinateVo.getMessage());
            } else {
                List<FenceCoordinate> fenceCoordinates = fenceCoordinateVo.getData();
                if (!CollectionUtils.isEmpty(fenceCoordinates)) {
                    for (FenceCoordinate fenceCoordinate : fenceCoordinates) {
                        fenceCoordinatesMap.put(fenceCoordinate.getGdId(), fenceCoordinate.getFenceCoordinate());
                    }
                }
            }
        }

        HttpUtils.Response response = HttpUtils.doPost(url, new JSONObject().toJSONString(), "utf-8", headerMap);
        if (response.getStatusCode() == 200) {
            String result = response.getResponseData();
            ConstructionVo constructionVo = JSONObject.parseObject(result, ConstructionVo.class);
            if (constructionVo.getStatus() != 200) {
                log.error(constructionVo.getMessage());
            } else {
                dpjfxtConstructionService.deleteAll();
                List<DpjfxtConstruction> dpjfxtConstructions = constructionVo.getData();
                if (!CollectionUtils.isEmpty(dpjfxtConstructions)) {
                    dpjfxtConstructions.forEach(
                            x -> {
                                if (fenceCoordinatesMap.containsKey(x.getGdId())) {
                                    x.setFenceCoordinate(fenceCoordinatesMap.get(x.getGdId()));
                                }else{
                                    x.setFenceCoordinate(null);
                                }
                                x.setCjsj(new Date());
                            }
                    );
                    dpjfxtConstructionService.saveBatch(dpjfxtConstructions);
                }
            }
        }
    }
}
